$(function(){
	  var user = JSON.parse(sessionStorage.getItem("user"));
	  $("#userName").text(user.userName);
	  
	  $.ajax({
		  url:'sys/userRoleResource',
		  type:'GET',
		  data:{"userName":user.userName},
		  success:function(data){
			  var treeData = toTreeData(data,"resourceId","parentId", "A00001","children");
			//定义一个存储拼接好的左侧菜单的字符串
  			var dynamicMenuStr = "";
			  for (var i = 0; i < treeData.length; i++) {
				  if(treeData[i].parentId == 'A00001'){
					  	dynamicMenuStr +="<li class='layui-nav-item'>" +
					  		" <a>" +
					  		"  <i class='ok-icon'>&#xe654;</i><cite>"+treeData[i].resourceName +"</cite><span class='layui-nav-more'></span></a> ";
					  	if (typeof(treeData[i].children) != "undefined" && treeData[i].children.length > 0) {
					  		dynamicMenuStr += "<dl class='layui-nav-child'>";
						  	for (var j = 0; j < treeData[i].children.length; j++) {
						  		dynamicMenuStr +="<dd>" +
								  		" <a lay-id='"+(i+2)+"-"+(j+1)+"' data-url='"+treeData[i].children[j].resourceUrl+"' is-close='true'>" +
								  		"  <i class='layui-icon'></i><cite>" 
								  		+ treeData[i].children[j].resourceName +"</cite></a></dd>";
						  	}
						  	dynamicMenuStr +="</dl>";
					  	}
					  	dynamicMenuStr +="</li>";
				  }
			  }
			  $("#navBar").append(dynamicMenuStr);
		  }
	  });
	  
	  function toTreeData(source, idName, parentIdName, parentId,childrenName){   
			var cloneData = JSON.parse(JSON.stringify(source));// 对源数据深度克隆
		    return cloneData.filter(father => {// 循环所有项，并添加children属性
		    	var branchArr = cloneData.filter(child => father[idName] == child[parentIdName]);// 返回每一项的子级数组
		        branchArr.length > 0 ? father[childrenName] = branchArr : '';//给父级添加一个children属性，并赋值
		        return father[parentIdName] == parentId;//返回第一层
		    });
	  };
  });